import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '@/store'
import { storeToRefs } from 'pinia'

// 消息提示
export const toast = (message, type = 'success', dangerouslyUseHTMLString = true) => {
  ElMessage({
    message,
    type,
    duration: 2000,
    dangerouslyUseHTMLString,
  })
}

// 确认框
export const showModal = (content = '提示内容', type = 'warning', title = '') =>
  ElMessageBox.confirm(content, title, {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type,
  })

// 弹出输入框
export const showPrompt = (tip, value = '') => {
  return ElMessageBox.prompt(tip, '', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    inputValue: value,
  })
}

// 修改头部层级
export const ChangeHeaderIndex = () => {
  const userStore = useUserStore()
  const { headerIndex } = storeToRefs(userStore)
  headerIndex.value = 0
}

// 恢复头部层级
export const resetHeaderIndex = () => {
  const userStore = useUserStore()
  const { headerIndex } = storeToRefs(userStore)
  headerIndex.value = 1
}

// 处理query参数
export const queryParams = (query) => {
  let querys = []
  for (const key in query) {
    if (query[key]) {
      querys.push(`${key}=${encodeURIComponent(query[key])}`)
    }
  }
  let result = querys.join('&')
  querys = querys ? '?' + querys : ''
  return querys
}

// 上移
export const useArrayMoveUp = (arr, index) => {
  swapArray(arr, index, index - 1)
}

// 下移
export const useArrayMoveDown = (arr, index) => {
  swapArray(arr, index, index + 1)
}

const swapArray = (arr, prev, next) => {
  arr[prev] = arr.splice(next, 1, arr[prev])[0]
  return arr
}

// sku排列算法
export function cartesianProductOf() {
  return Array.prototype.reduce.call(arguments, function (a, b) {
    var ret = [];
    a.forEach(function (a) {
      b.forEach(function (b) {
        ret.push(a.concat([b]));
      });
    });
    return ret;
  }, [
    []
  ]);
}
